package zone.czh.woi.woim.server.channel.initializer.manager;


import io.netty.handler.timeout.IdleStateHandler;
import zone.czh.woi.woim.base.channel.adapter.ProtocolAdapter;
import zone.czh.woi.woim.base.channel.codec.PayloadEncoder;
import zone.czh.woi.woim.base.channel.codec.PacketHandler;
import zone.czh.woi.woim.base.channel.codec.PayloadHandler;
import zone.czh.woi.woim.base.channel.protocol.selector.ProtocolSelector;
import zone.czh.woi.woim.base.constant.WOIMConfig;
import zone.czh.woi.woim.server.WOIMServer;
import zone.czh.woi.woim.server.channel.handler.*;
import zone.czh.woi.woim.server.channel.protocol.initializer.ProtocolInitializer;
import zone.czh.woi.woim.server.channel.protocol.builder.ServerProtocolBuilder;

import java.util.List;
import java.util.Map;

/**
*@ClassName: HandlerManager
*@Description: None
*@author woi
*/
public class HandlerManager {
    private WOIMServer woimServer;
    private WOIMServer.Configurator configurator;

    ConnectionStateHandler connectionStateHandler;
    PayloadEncoder payloadEncoder;
    PrivateMsgHandler privateMsgHandler;
    GroupMsgHandler groupMsgHandler;
    PrivateMsgEncoder privateMsgEncoder;
    GroupMsgEncoder groupMsgEncoder;
    IntentEncoder intentEncoder;
    Barrier barrier;
    PayloadHandler payloadHandler;
    Map<String,ProtocolAdapter> protocolAdapters;
    ProtocolSelector protocolSelector;
    public HandlerManager(WOIMServer woimServer,WOIMServer.Configurator configurator){
        this.woimServer = woimServer;
        this.configurator = configurator;
        connectionStateHandler = new ConnectionStateHandler(woimServer.getEventListener());
        payloadEncoder =new PayloadEncoder();
        payloadEncoder.setProtocolSelector(configurator.getProtocolSelector());
        privateMsgHandler = new PrivateMsgHandler(configurator.getPrivateMsgService());
        groupMsgHandler = new GroupMsgHandler(configurator.getGroupMsgService());
        privateMsgEncoder = new PrivateMsgEncoder();
        groupMsgEncoder = new GroupMsgEncoder();
        intentEncoder = new IntentEncoder();
        barrier = new Barrier();
        payloadHandler=new PayloadHandler();
        protocolAdapters = configurator.getProtocolAdapters();
        protocolSelector = configurator.getProtocolSelector();
    }

    public IdleStateHandler idleStateHandler(){
        return new IdleStateHandler(WOIMConfig.READER_IDLE_TIME_SECONDS,0,0);
    }

    public ConnectionStateHandler connectionStateHandler(){
        return connectionStateHandler;
    }

    public AuthHandler authHandler(){
         AuthHandler authHandler = new AuthHandler().setVerifier(configurator.getChannelVerifier())
                .setEventListener(woimServer.getEventListener())
                .setWoimServer(woimServer);
        return authHandler;
    }

    public Barrier barrier(){
        return  barrier;
    }

    public PayloadHandler payloadHandler(){
        return payloadHandler;
    }

    public PayloadEncoder payloadEncoder(){
        payloadEncoder.setAdapters(protocolAdapters);
        payloadEncoder.setProtocolSelector(protocolSelector);
        return payloadEncoder;
    }

    public PrivateMsgHandler privateMsgHandler(){
        return privateMsgHandler;
    }

    public GroupMsgHandler groupMsgHandler(){
        return groupMsgHandler;
    }

    public PrivateMsgEncoder privateMsgEncoder(){
        return privateMsgEncoder;
    }

    public GroupMsgEncoder groupMsgEncoder(){
        return groupMsgEncoder;
    }

    public IntentEncoder intentEncoder(){
        return intentEncoder;
    }

    public List<PacketHandler> customHandlers(){
        return configurator.getCustomHandler();
    }


    public ProtocolInitializer protocolInitializer() {
        ProtocolInitializer protocolInitializer = new ProtocolInitializer()
                .setProtocolDetector(configurator.getProtocolDetector())
                .setProtocolBuilder(new ServerProtocolBuilder(protocolAdapters));
        return protocolInitializer;
    }
}
